package Arkkomeet
import java.sql.Time;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.NumberFormat
import java.text.SimpleDateFormat;
import java.util.Formatter.Conversion;
import java.util.Formatter.DateTime;
import javax.swing.text.DateFormatter;
import org.apache.jasper.tagplugins.jstl.core.Param;
import org.hibernate.sql.Select;
import javax.persistence.Id;
import org.apache.tools.ant.taskdefs.optional.Cab;
import org.springframework.dao.DataIntegrityViolationException
import javax.persistence.Id;
import org.apache.tools.ant.taskdefs.optional.Cab;

import arkkomeet.MinutaService;



class MinutaController {
	def minutaParticipanteXMinutaService
	def minutaAsuntosService
	def minutaService
	def minutaArchivosService

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	def index() {
		redirect(action: "list", params: params)
	}
	
	def list(Integer max) {
		params.max = Math.min(max ?: 10, 100)
		[minutaInstanceList: Minuta.list(params), minutaInstanceTotal: Minuta.count()]
	}

	def create() {
		[minutaInstance: new Minuta(params)]
		[asuntoInstance: new Asunto(params)]
		[participanteXMinutaInstance: new ParticipanteXMinuta(params)]
	}

	def createEncabezado() {
		flash.valFechaReunionBlank=""
		flash.valHoraInicioBlank=""
		flash.valLugarBlank=""
		flash.valProyectoBlank=""
		flash.valTipoReunionBlank=""
		flash.valPropositoBlank=""
		def ultimo= minutaService.obtenerUltimo()
		DateFormat dfDateMedium = DateFormat.getDateInstance(DateFormat.MEDIUM);
		SimpleDateFormat formatoHora = new SimpleDateFormat("HH:mm");
		[minutaInstance: new Minuta(params), ultimo:ultimo,formato:dfDateMedium, formatoHora:formatoHora]
	}
	
	def save() {
		flash.valFechaReunionBlank=""
		flash.valHoraInicioBlank=""
		flash.valLugarBlank=""
		flash.valProyectoBlank=""
		flash.valTipoReunionBlank=""
		flash.valPropositoBlank=""
		String usuario = session.user.login
		Minuta minutaInstance = minutaService.guardarMinuta(params,usuario)
		if (minutaInstance.hasErrors()) {
			int ultimo = Minuta.count()
			ultimo = ultimo+1
			SimpleDateFormat formatoHora = new SimpleDateFormat("HH:mm");
			Long idTipoReunion= minutaInstance.getTipoReunion().id
			DateFormat dfDateMedium = DateFormat.getDateInstance(DateFormat.MEDIUM);

			if(minutaInstance.minFechaReunion.equals(null)){
				flash.valFechaReunionBlank = message(code: 'Minuta.minFechaReunion.blank')
			}
			if(minutaInstance.minHoraInicio.equals(null)){
				flash.valHoraInicioBlank = message(code: 'Minuta.minHoraInicio.blank')

			}
			if(minutaInstance.minLugar.equals("")){
				flash.valLugarBlank=message(code: 'Minuta.minLugar.blank')

			}
			if(minutaInstance.minProyectoAsociado.equals("")){
				flash.valProyectoBlank=message(code: 'Minuta.minProyectoAsociado.blank')
			}
			if(params.tipoReunion.equals("null")){
				flash.valTipoReunionBlank=message(code: 'Minuta.tipoReunion.nullable')
			}
			if(minutaInstance.minPropositoReunion.equals("")){
				flash.valPropositoBlank=message(code: 'Minuta.minPropositoReunion.blank')
			}
			render(view: "createEncabezado", model: [minutaInstance: minutaInstance,ultimo:ultimo,formato:dfDateMedium, formatoHora:formatoHora])
			return

		}

		flash.message = message(code: 'default.created.message', args: [message(code: 'minuta.label', default: 'Minuta'), minutaInstance.id])
		redirect(action: "edit", id: minutaInstance.id)
	}
	
	def show(Long id) {
		def minutaInstance = Minuta.get(id)
		if (!minutaInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'minuta.label', default: 'Minuta'), id])
			redirect(action: "list")
			return
		}
		[minutaInstance: minutaInstance]
	}
	
	def edit(Long id) {
		def minutaInstance = Minuta.get(id)
		DateFormat dfDateMedium = DateFormat.getDateInstance(DateFormat.MEDIUM);
		String formatoFecha = dfDateMedium.format(minutaInstance.getMinFechaReunion())
		SimpleDateFormat formatoHora = new SimpleDateFormat("HH:mm");
		Long idTipoReunion= minutaInstance.getTipoReunion().id
		int ultimo=minutaInstance.id
		if (!minutaInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'minuta.label', default: 'Minuta'), id])
			redirect(action: "list")
			return
		}
		ArrayList<CatalogoParticipante> listParticipantes= minutaParticipanteXMinutaService.cargarParticipantes(minutaInstance)

		[minutaInstance: minutaInstance, formato:dfDateMedium,formatoHora:formatoHora,ultimo:ultimo,idTipoReunion:idTipoReunion, listParticipantes: listParticipantes]
		//[minutaInstance: minutaInstance, formato:dfDateMedium,formatoHora:formatoHora,ultimo:ultimo,idTipoReunion:idTipoReunion]

	}

	def update(Long id, Long version) {
		flash.valFechaReunionBlank=""
		flash.valHoraInicioBlank=""
		flash.valLugarBlank=""
		flash.valProyectoBlank=""
		flash.valTipoReunionBlank=""
		flash.valPropositoBlank=""
		def minutaInstance = Minuta.get(id)
		if (!minutaInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'minuta.label', default: 'Minuta'), id])
			redirect(action: "list")
			return
		}
		if (version != null) {
			if (minutaInstance.version > version) {
				minutaInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
						[message(code: 'minuta.label', default: 'Minuta')] as Object[],
						"Another user has updated this Minuta while you were editing")
				render(view: "edit", model: [minutaInstance: minutaInstance])
				return
			}
		}
		String usuario = session.user.login

		minutaInstance.properties = minutaService.modificarMinuta(params, usuario) //params

		if (!minutaInstance.save(flush: true)) {
			int ultimo = Minuta.count()
			ultimo =minutaInstance.id
			SimpleDateFormat formatoHora = new SimpleDateFormat("HH:mm");
			Long idTipoReunion= minutaInstance.getTipoReunion().id
			DateFormat dfDateMedium = DateFormat.getDateInstance(DateFormat.MEDIUM);

			if(minutaInstance.minFechaReunion.equals(null)){
				flash.valFechaReunionBlank = message(code: 'Minuta.minFechaReunion.blank')
			}
			if(minutaInstance.minHoraInicio.equals(null)){
				flash.valHoraInicioBlank = message(code: 'Minuta.minHoraInicio.blank')

			}
			if(minutaInstance.minLugar.equals("")){
				flash.valLugarBlank=message(code: 'Minuta.minLugar.blank')

			}
			if(minutaInstance.minProyectoAsociado.equals("")){
				flash.valProyectoBlank=message(code: 'Minuta.minProyectoAsociado.blank')
			}
			if(params.tipoReunion.equals("null")){
				flash.valTipoReunionBlank=message(code: 'Minuta.tipoReunion.nullable')
			}
			if(minutaInstance.minPropositoReunion.equals("")){
				flash.valPropositoBlank=message(code: 'Minuta.minPropositoReunion.blank')
			}
			render(view: "edit", model: [minutaInstance: minutaInstance,ultimo:ultimo,formato:dfDateMedium, formatoHora:formatoHora])
			return
		}
		flash.message = message(code: 'default.updated.message', args: [message(code: 'minuta.label', default: 'Minuta'), minutaInstance.id])
		redirect(action: "edit", id:minutaInstance.id)
		return
	}

	def delete(Long id) {
		def minutaInstance = Minuta.get(id)
		if (!minutaInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'minuta.label', default: 'Minuta'), id])
			redirect(action: "list")
			return
		}
		try {
			minutaInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'minuta.label', default: 'Minuta'), id])
			redirect(action: "list")
		}
		catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'minuta.label', default: 'Minuta'), id])
			redirect(action: "show", id: id)
		}
	}
	
	def guardarArchivo(params){
		def archivo = minutaArchivosService.guardarArchivo(params)
		render(view: "show", model: [archivo: archivo])
		return
	}

	def agregarParticipante(Long id, Long idMin){
		def tempParticipante = params.cbxParticipantes
		def partID = Long.parseLong(tempParticipante)
		def listParticipantes= minutaParticipanteXMinutaService.agregarParticipantes(partID, idMin)
		redirect(action: "edit",id:idMin)
		return
	}
	
	def quitarParticipante(Long id){
		def listParticipantes= minutaParticipanteXMinutaService.quitarParticipante(id)
		render(view: "create", model: [listParticipantes: listParticipantes])
		return
	}
	
	def guardarParticipantes(Long id){
		minutaParticipanteXMinutaService.guardarParticipantes()
	}
	
	def agregarAsunto(){
		def asuntoInstance = new Asunto(params)
		def minutaInstance = new Minuta()
		asuntoInstance.addToMinuta(minutaInstance)
		if (!asuntoInstance.save(flush: true)) {
			render(view: "create", model: [asuntoInstance: asuntoInstance])
			return
		}

		def listAsuntos= minutaAsuntosService.agregarAsunto()
		render(view: "create", model: [listAsuntos: listAsuntos])
		return
	}
	def eliminarAsunto(){
	}

}
